package com.jiao.Controller;

import com.jiao.Exception.BizException;
import com.jiao.domain.DTO.RegisterDto;
import com.jiao.domain.Result;
import com.jiao.domain.VO.UserInfoVo;
import com.jiao.enums.ExceptionCodeEnum;
import com.jiao.service.SysUserService;
import com.jiao.service.UploadImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Objects;

@RestController
@RequestMapping("/user")
public class UserController {

    /*
    * 使用@Autowired导入对象不行，是因为idea不推荐对于私有方法使用set的方法注入，而是推荐使用构造方法
    * */
    @Autowired
    private SysUserService userService;
    @Resource(name = "OssUploadImgService")
    private UploadImgService uploadImgService;

    /*
    * 进入个人中心的时候需要能够查看当前用户信息
    * */
    @GetMapping("/userInfo")
    public Result<UserInfoVo> selectUserInfo(){
        return userService.userInfo();
    }

    @PostMapping("/uploadImg")
    public Result<String> uploadImg(MultipartFile img) throws IOException {
        return uploadImgService.uploadImg(img);
    }

    @PostMapping("/updateUserInfo")
    public Result<Void> updateUserInfo(@RequestBody  UserInfoVo userInfoVo){
        return userService.updateUserInfo(userInfoVo);
    }

    @GetMapping("/follow")
    public Result<Boolean> follow(Long followerId){
        if(Objects.isNull(followerId))
        {
            throw new BizException(ExceptionCodeEnum.SYSTEM_ERROR);
        }
        return userService.follow(followerId);
    }

    @PostMapping("/register")
    public Result<Boolean> register(@Validated @RequestBody RegisterDto registerDto){
        return userService.register(registerDto);
    }
}